package com.ymatou.iapi.productquery.testcase;

import com.google.common.reflect.TypeToken;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.LiveActivityDto;
import com.ymatou.iapi.prodstock.parameter.PreOrderReduceCatalogDto;
import com.ymatou.iapi.productmanager.parameter.SaveGrouponBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.parameter.product.CatalogSaveModel;
import com.ymatou.iapi.productmanager.parameter.productdetail.*;
import com.ymatou.iapi.productquery.parameter.GetProductDetailListByTradeIsolationBean;
import com.ymatou.iapi.productquery.service.GetProductDetailListByTradeIsolationCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.*;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.database.mongo.jproduct.CatalogsWapper;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.database.mongo.jproduct.ProductsWapperV2;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Ts_GetProductDetailListByTradeIsolation {
	private static GetProductDetailListByTradeIsolationCall getProductDetailListByTradeIsolationCall;
	private static GetProductDetailListByTradeIsolationBean getProductDetailListByTradeIsolationBean;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	//vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	//vip渠道->代理1
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
			
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("交易隔离查询接口, 获取商品详细信息");
	}

	@Before
	public void caseUp() {
		getProductDetailListByTradeIsolationCall = new GetProductDetailListByTradeIsolationCall();
		getProductDetailListByTradeIsolationBean = new GetProductDetailListByTradeIsolationBean();
	}
	

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductDetailListByTradeIsolationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductDetailListByTradeIsolation_001() {
		Logger.start(true, "普通单规格商品，普通单规格直播商品 查询");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			Logger.comment("修改商品规格信息,设置新客价格最便宜");
			// 场景描述1规格新客价格最便宜
			addpro.getProduct().getCatalogList().remove(1);
			CatalogSaveModel c = addpro.getProduct().getCatalogList().get(0);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Integer activityId = ActivityCallService.createIsolationActivityInProcess();
			Double ap = 3.0d;
			Thread.sleep(2000);
			List<String> ProductIds = new ArrayList<String>();
			ProductIds.add(productId);
			getProductDetailListByTradeIsolationBean.setProductIdList(ProductIds);
			getProductDetailListByTradeIsolationBean.setNextActivityExpire(10);
			getProductDetailListByTradeIsolationCall.setData(getProductDetailListByTradeIsolationBean);
			getProductDetailListByTradeIsolationCall.callService();
			Logger.verifyEquals("200", getProductDetailListByTradeIsolationCall.getString("Code"), "返回码检查");
			getProductDetailListByTradeIsolationCall.getProductList().forEach(product -> {
				try {
					verifyDB(product, new ArrayList<String>(), 0);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductDetailListByTradeIsolation_002() {
		Logger.start(true, "普通多规格商品,普通多规格直播商品 查询");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			Logger.comment("修改商品规格信息,设置新客价格最便宜");
			// 场景描述1规格新客价格最便宜
			CatalogSaveModel c = addpro.getProduct().getCatalogList().get(0);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Integer activityId = ActivityCallService.createIsolationActivityInProcess();
			Double ap = 3.0d;
			// 活动价格
			// List<String> inaids =
			// ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,
			// catalogs, activityId, 10, ap.toString());
			// ProductManagerCallServiceV2.approveProductInActivity(productId,
			// inaids.get(0), true);
			Thread.sleep(2000);
			List<String> ProductIds = new ArrayList<String>();
			ProductIds.add(productId);
			getProductDetailListByTradeIsolationBean.setProductIdList(ProductIds);
			getProductDetailListByTradeIsolationBean.setNextActivityExpire(10);
			getProductDetailListByTradeIsolationCall.setData(getProductDetailListByTradeIsolationBean);
			getProductDetailListByTradeIsolationCall.callService();
			Logger.verifyEquals("200", getProductDetailListByTradeIsolationCall.getString("Code"), "返回码检查");
			getProductDetailListByTradeIsolationCall.getProductList().forEach(product -> {
				try {
					verifyDB(product, new ArrayList<String>(), 0);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductDetailListByTradeIsolation_003() {
		Logger.start(true, "非交易隔离活动商品查询");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(1);
			CatalogSaveModel c = addpro.getProduct().getCatalogList().get(0);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Integer activityId = ActivityCallService.createNoIsolationActivityInProcess();
			Double ap = 3.0d;
			// 活动价格
			List<String> inaids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId, catalogs, activityId, 10, ap.toString());
			ProductManagerCallServiceV2.approveProductInActivity(productId, inaids.get(0), true);
			Thread.sleep(2000);
			List<String> ProductIds = new ArrayList<String>();
			ProductIds.add(productId);
			getProductDetailListByTradeIsolationBean.setProductIdList(ProductIds);
			getProductDetailListByTradeIsolationBean.setNextActivityExpire(10);
			getProductDetailListByTradeIsolationCall.setData(getProductDetailListByTradeIsolationBean);
			getProductDetailListByTradeIsolationCall.callService();
			Logger.verifyEquals("200", getProductDetailListByTradeIsolationCall.getString("Code"), "返回码检查");
			getProductDetailListByTradeIsolationCall.getProductList().forEach(product -> {
				try {
					verifyDB(product, inaids, activityId);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductDetailListByTradeIsolation_004() {
		Logger.start(true, "交易隔离活动商品查询");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			Logger.comment("修改商品规格信息,设置新客价格最便宜");
			// 场景描述1规格新客价格最便宜
			addpro.getProduct().getCatalogList().remove(1);
			CatalogSaveModel c = addpro.getProduct().getCatalogList().get(0);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Integer activityId = ActivityCallService.createIsolationActivityInProcess();
			Double ap = 3.0d;
			// 活动价格
			List<String> inaids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId, catalogs, activityId, 10, ap.toString());
			ProductManagerCallServiceV2.approveProductInActivity(productId, inaids.get(0), true);
			Thread.sleep(2000);
			List<String> ProductIds = new ArrayList<String>();
			ProductIds.add(productId);
			getProductDetailListByTradeIsolationBean.setProductIdList(ProductIds);
			getProductDetailListByTradeIsolationBean.setNextActivityExpire(10);
			getProductDetailListByTradeIsolationCall.setData(getProductDetailListByTradeIsolationBean);
			getProductDetailListByTradeIsolationCall.callService();
			Logger.verifyEquals("200", getProductDetailListByTradeIsolationCall.getString("Code"), "返回码检查");
			getProductDetailListByTradeIsolationCall.getProductList().forEach(product -> {
				try {
					verifyDB(product, inaids, activityId);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductDetailListByTradeIsolation_005() {
		Logger.start(true, "活动商品售罄查询");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(1);
			CatalogSaveModel c = addpro.getProduct().getCatalogList().get(0);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Integer activityId = ActivityCallService.createNoIsolationActivityInProcess();
			Double ap = 3.0d;
			// 活动价格
			List<String> inaids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId, catalogs, activityId, 10, ap.toString());
			ProductManagerCallServiceV2.approveProductInActivity(productId, inaids.get(0), true);

			Thread.sleep(2000);
			try {
				PreOrderReduceCatalogDto preOrderReduceCatalogDto = ProdStockCallService.initPreOrderReduceCatalogDto(activityId, catalogs.get(0), 10);
				ProdStockCallService.ReduceStockListForPreOrder(preOrderReduceCatalogDto);
			} catch (Exception e) {
				// TODO: handle exception
			}
			Thread.sleep(2000);
			List<String> ProductIds = new ArrayList<String>();
			ProductIds.add(productId);
			getProductDetailListByTradeIsolationBean.setProductIdList(ProductIds);
			getProductDetailListByTradeIsolationBean.setNextActivityExpire(10);
			getProductDetailListByTradeIsolationCall.setData(getProductDetailListByTradeIsolationBean);
			getProductDetailListByTradeIsolationCall.callService();
			Logger.verifyEquals("200", getProductDetailListByTradeIsolationCall.getString("Code"), "返回码检查");
			getProductDetailListByTradeIsolationCall.getProductList().forEach(product -> {
				try {
					verifyDB(product, inaids, activityId);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductDetailListByTradeIsolation_006() {
		Logger.start(true, "秒杀活动查询");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(1);
			CatalogSaveModel c = addpro.getProduct().getCatalogList().get(0);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomWapper LiveRoomWapper = new LiveRoomWapper();
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			LiveActivityDto LiveActivity=createliveactivityBean.getLiveActivity();
			Integer LiveActivityProductId = LiveRoomWapper.selectLiveactivityproduct(liveacctivityid, LiveActivity.getActivityProducts().get(0).getProductId()).get(0).getLiveActivityProductId();
			Thread.sleep(10000);
			List<String> ProductIds = new ArrayList<String>();
			ProductIds.add(productId);
			getProductDetailListByTradeIsolationBean.setProductIdList(ProductIds);
			getProductDetailListByTradeIsolationBean.setNextActivityExpire(10);
			getProductDetailListByTradeIsolationCall.setData(getProductDetailListByTradeIsolationBean);
			getProductDetailListByTradeIsolationCall.callService();
			Logger.verifyEquals("200", getProductDetailListByTradeIsolationCall.getString("Code"), "返回码检查");
			getProductDetailListByTradeIsolationCall.getProductList().forEach(product -> {
				try {
					List<String> pins=new ArrayList<String>();
					pins.add(LiveActivityProductId.toString());
					verifyLiveDB(product, pins, liveacctivityid);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductDetailListByTradeIsolation_007() {
		Logger.start(true, "拼团商品查询");
		try {
			List<String> productList = new ArrayList<>();

			for (int i=0;i<6;i++){
				AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
				setStock(addpro, 30);
				setPrice(addpro, 100d);
				String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
				Double groupPrice = 50d;
				SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(3383,pid,groupPrice);
				ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
				productList.add(pid);
			}
			Thread.sleep(2000);
			getProductDetailListByTradeIsolationBean.setProductIdList(productList);
			getProductDetailListByTradeIsolationBean.setNextActivityExpire(10);
			getProductDetailListByTradeIsolationCall.setData(getProductDetailListByTradeIsolationBean);
			getProductDetailListByTradeIsolationCall.callService();
			Logger.verifyEquals("200", getProductDetailListByTradeIsolationCall.getString("Code"), "返回码检查");
			getProductDetailListByTradeIsolationCall.getProductList().forEach(product -> {
				try {
					verifyGrouponDB(product);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductDetailListByTradeIsolation_008() {
		Logger.start(true, "带推荐语商品查询");
		try {
			List<String> productList = new ArrayList<>();

			for (int i=0;i<6;i++){
				AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
				addpro.getProduct().setRecomReason("推荐推荐");
				setStock(addpro, 30);
				setPrice(addpro, 100d);
				String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
				Double groupPrice = 50d;
				SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(3383,pid,groupPrice);
				ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
				productList.add(pid);
			}
			Thread.sleep(2000);
			getProductDetailListByTradeIsolationBean.setProductIdList(productList);
			getProductDetailListByTradeIsolationBean.setNextActivityExpire(10);
			getProductDetailListByTradeIsolationCall.setData(getProductDetailListByTradeIsolationBean);
			getProductDetailListByTradeIsolationCall.callService();
			Logger.verifyEquals("200", getProductDetailListByTradeIsolationCall.getString("Code"), "返回码检查");
			getProductDetailListByTradeIsolationCall.getProductList().forEach(product -> {
				try {
					verifyGrouponDB(product);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductDetailListByTradeIsolation_009() {
		Logger.start(true, "价格服务增加币种");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(3000);
			getProductDetailListByTradeIsolationBean.setProductIdList(Arrays.asList(productId));
			getProductDetailListByTradeIsolationBean.setNextActivityExpire(10);
			getProductDetailListByTradeIsolationCall.setData(getProductDetailListByTradeIsolationBean);
			getProductDetailListByTradeIsolationCall.callService();
			Logger.verifyEquals("200", getProductDetailListByTradeIsolationCall.getString("Code"), "返回码检查");
			JsonObject product = getProductDetailListByTradeIsolationCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("ProductList").get(0).getAsJsonObject();
			Logger.verifyNotNull(product.getAsJsonObject("CurrencyExchangeRate").get("OnshoreExchangeRate").getAsBigDecimal().doubleValue(),"商品标价货币和参考汇率--美元");
			Logger.verifyEquals(product.getAsJsonObject("MarketCurrencyExchangeRate").get("OnshoreExchangeRate").getAsBigDecimal().doubleValue(),1.0,"商品市场价货币和参考汇率-人民币");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductDetailListByTradeIsolation_010() {
		Logger.start(true, "仅支持自提的商品");
		try {
			
			int rootUserId = 501465799;
			int agentUserId = 501475678;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId, "",
					true);
			addAgentProductBean.setExpressDelivery(false);
			addAgentProductBean.setMinOrderNum(20);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(3000);
			
			getProductDetailListByTradeIsolationBean.setProductIdList(Arrays.asList(productId));
			getProductDetailListByTradeIsolationBean.setNextActivityExpire(10);
			getProductDetailListByTradeIsolationCall.setData(getProductDetailListByTradeIsolationBean);
			getProductDetailListByTradeIsolationCall.callService();
			Logger.verifyEquals("200", getProductDetailListByTradeIsolationCall.getString("Code"), "返回码检查");
			JsonObject product = getProductDetailListByTradeIsolationCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("ProductList").get(0).getAsJsonObject();
			Logger.verifyEquals(false,product.getAsJsonObject("DeliveryPattern").get("ExpressDelivery").getAsBoolean(),"验证快递方式");
			Logger.verifyEquals(true,product.getAsJsonObject("DeliveryPattern").get("CollectionGoods").getAsBoolean(),"验证自提");
			Logger.verifyEquals(20,product.get("minOrderNum").getAsInt(),"验证最小起购数量");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductDetailListByTradeIsolation_011() {
		Logger.start(true, "查询增加商品发布类型");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addpro.getProduct().setPublishType(2);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			Thread.sleep(4000);
			getProductDetailListByTradeIsolationBean.setProductIdList(Arrays.asList(productId));
			getProductDetailListByTradeIsolationBean.setNextActivityExpire(10);
			getProductDetailListByTradeIsolationCall.setData(getProductDetailListByTradeIsolationBean);
			getProductDetailListByTradeIsolationCall.callService();
			Logger.verifyEquals("200", getProductDetailListByTradeIsolationCall.getString("Code"), "返回码检查");
			JsonObject product = getProductDetailListByTradeIsolationCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("ProductList").get(0).getAsJsonObject();
			Logger.verifyEquals(product.get("PublishType").getAsInt(),2,"验证PublishType");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	

	private void setStock(AddSellerProductRequest addpro, Integer catalogNum) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setStock(catalogNum);
		});
	}

	private void setPrice(AddSellerProductRequest addpro, Double price) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setNewPrice(price);
			catalog.setMarketPrice(price);
			catalog.setVipPrice(price);
			catalog.setPrice(price);
		});
	}

	private void verifyGrouponDB(ProductDetailDto2 product) throws Exception {
		String productId = product.getProductId();
		Logger.comment("开始校验商品信息Products");
		Map<String, Object> productMap = new ProductsWapperV2().getByProductId(productId);
		// 允许时间差值 5s
		long candiff = 5 * 1000;
		// 组装数据
		// - 商品信息
		Logger.verifyEquals(product.getProdId(), productMap.get("ipid"), "验证ipid");

		Logger.verifyEquals(product.getVersion(), String.valueOf(Double.valueOf(productMap.get("ver").toString()) / 1000), "验证ver");

		Logger.verifyEquals(product.getTitle(), productMap.get("title").toString(), "验证title");
		Logger.verifyEquals(product.getIntegerroduction(), productMap.get("intro"), "验证intro");

		Logger.verifyEquals(true, vdate(product.getValidStart(), (Date) productMap.get("start"), candiff), "验证start");

		Logger.verifyEquals(true, vdate(product.getValidEnd(), (Date) productMap.get("end"), candiff), "验证end");

		
		Logger.verifyEquals(product.getSellerId(), productMap.get("sid"), "验证sid");
		Logger.verifyEquals(product.getSellerName(), productMap.get("sname"), "验证sname");
		Logger.verifyEquals(product.getBrandId(), productMap.get("bid"), "验证bid");
		Logger.verifyEquals(product.getBrandName(), productMap.get("brand"), "验证brand");
		Logger.verifyEquals(product.getBrandEnName(), productMap.get("ebrand"), "验证ebrand");
		Logger.verifyEquals(product.getMasterCategoryId(), productMap.get("mcatid"), "验证mcatid");
		Logger.verifyEquals(product.getMasterCategoryName(), productMap.get("mcatname"), "验证mcatname");
		Logger.verifyEquals(product.getCategoryId(), productMap.get("scatid"), "验证scatid");
		Logger.verifyEquals(product.getCategoryName(), productMap.get("scatname"), "验证scatname");
		Logger.verifyEquals(product.getThirdCategoryId(), productMap.get("tcatid"), "验证tcatid");
		Logger.verifyEquals(product.getThirdCategoryName(), productMap.get("tcatname"), "验证tcatname");
		Logger.verifyEquals(product.getCatalogType(), productMap.get("ctype"), "验证ctype");
		Logger.verifyEquals(product.getDeliveryMethod(), productMap.get("deliv"), "验证deliv");
		Logger.verifyEquals(product.getBondedArea(), productMap.get("bonded"), "验证bonded");
		Logger.verifyEquals(product.getTariffType(), productMap.get("tariffy"), "验证tariffy");
		Logger.verifyEquals(product.getIsFreeShipping(), Double.parseDouble(productMap.get("shipping").toString()) <= 0, "验证tariffy");

		Logger.verifyEquals(product.getCountryId(), productMap.get("country"), "验证country");
		Logger.verifyEquals(true, vdate(product.getAddTime(), (Date) productMap.get("addtime"), candiff), "验证addtime");
		Logger.verifyEquals(product.getLocalReturn(), productMap.get("localr"), "验证localr");
		Logger.verifyEquals(product.getNoticeRisk(), productMap.get("risk"), "验证risk");
		Logger.verifyEquals(product.getProductCode(), productMap.get("pcode") == null ? "" : productMap.get("pcode").toString(), "验证pcode");
		Logger.verifyEquals(product.getNoReasonReturn(), productMap.get("noreason"), "验证noreason");
		
		//add by yangyutong 2020-02-16
		Logger.verifyEquals(product.getRecomReason(), productMap.get("recomreason"), "验证推荐语");
		
		Logger.verifyEquals(product.getIsNewProduct(), productMap.get("isnew"), "验证isnew");
		Logger.verifyEquals(product.getGrouponVersion(), Integer.parseInt(productMap.get("goversion").toString()), "验证goversion");

		Logger.verifyEquals(true, vdate(product.getNewStartTime(), (Date) productMap.get("newstart"), candiff), "验证newstart");

		Logger.verifyEquals(true, vdate(product.getNewEndTime(), (Date) productMap.get("newend"), candiff), "验证newend");
		Logger.verifyEquals(product.getIsHotRecmd(), productMap.get("istop"), "验证istop");
		Logger.verifyEquals(product.getIsPspProduct(), productMap.get("ispsp"), "验证ispsp");
		Logger.verifyEquals(product.getGrade(), productMap.get("grade"), "验证grade");
		Logger.verifyEquals(product.getHasTextDescription(), productMap.get("newdesc"), "验证newdesc");
		if (productMap.containsKey("v") && productMap.get("v") != null) {
			JSONObject jsonObject = new JSONObject(productMap.get("v").toString());
			//
			Logger.verifyEquals(product.getVideo().getVideoUrl(), jsonObject.getString("u"), "验证u");
			Logger.verifyEquals(product.getVideo().getVideoLength(), jsonObject.getString("l"), "验证l");
			Logger.verifyEquals(product.getVideo().getVideoWidth(), jsonObject.getString("w"), "验证w");
			Logger.verifyEquals(product.getVideo().getVideoHeight(), jsonObject.getString("h"), "验证h");
			Logger.verifyEquals(product.getVideo().getVideoSize(), jsonObject.getString("s"), "验证s");

		}
		// // pic list
		List<String> picList = new ArrayList<>();
		if (productMap.get("pics") != null) {
			JSONArray jsonArray = new JSONArray(productMap.get("pics").toString());
			for (int i = 0; i < jsonArray.length(); i++) {
				picList.add(jsonArray.getString(i));
			}
		}
		Logger.verifyEquals(product.getPicList(), picList, "验证picList");

		List<String> sizePics = new ArrayList<>();
		if (productMap.get("sizepics") != null) {
			JSONArray jsonArray = new JSONArray(productMap.get("sizepics").toString());
			for (int i = 0; i < jsonArray.length(); i++) {
				sizePics.add(jsonArray.getString(i));
			}
		}
		Logger.verifyEquals(product.getSizePicList(), sizePics, "验证sizePics");

		Logger.verifyEquals(product.getOwnProduct(), Integer.parseInt(productMap.get("own").toString()) == 1 ? true : false, "验证own");

		Logger.verifyEquals(product.getExtraDeliveryType(), productMap.get("mdeliv"), "验证mdeliv");
		Logger.verifyEquals(product.getExtraDeliveryFee(), productMap.get("mflight"), "验证mflight");
		// 规格信息
		List<CatalogDto> catalist = product.getCatalogList();
		Map<String, Map> mongocatalist = new HashMap<String, Map>();
		new CatalogsWapper().getAllByProductId(productId).stream().forEach(map -> {
			mongocatalist.put(map.get("cid").toString(), map);
		});
		Logger.verifyEquals(catalist.size(), mongocatalist.size(), "验证规格size");
		for (int i = 0; i < catalist.size(); i++) {
			CatalogDto rcata = catalist.get(i);
			Map mcata = mongocatalist.get(rcata.getCatalogId());
			Logger.verifyNotNull(mcata, "验证mongo有数据");
			Logger.verifyEquals(rcata.getSKU(), mcata.get("sku"), "验证sku");
			Logger.verifyEquals(rcata.getPrice(), mcata.get("price"), "验证price");
			Logger.verifyEquals(rcata.getNewGuestPrice(), mcata.get("newp"), "验证newp");
			Logger.verifyEquals(rcata.getVipPrice(), mcata.get("vip"), "验证vip");
			List<Map> mprops = (List<Map>) mcata.get("props");
			Logger.verifyEquals(rcata.getPropertyList().size(), mprops.size(), "验证PropertyList.size");
			List<String> names = rcata.getPropertyList().stream().map(CatalogPropertyDto::getName).collect(Collectors.toList());
			List<String> values = rcata.getPropertyList().stream().map(CatalogPropertyDto::getValue).collect(Collectors.toList());
			List<String> pics = rcata.getPropertyList().stream().map(CatalogPropertyDto::getPicUrl).collect(Collectors.toList());
			mprops.stream().forEach(mprop -> {
				Logger.verifyEquals(true, names.contains(mprop.get("name")), "验证name");
				Logger.verifyEquals(true, values.contains(mprop.get("value")), "验证value");
				Logger.verifyEquals(true, pics.contains(mprop.get("pic")), "验证pic");
			});
		}

	}

	/**
	 * 秒杀用
	 * @param product
	 * @param arrayList
	 * @param liveacctivityid
	 * @throws Exception 
	 */
	private void verifyLiveDB(ProductDetailDto2 product, List<String> pinaids, Integer liveacctivityid) throws Exception {
		String productId = product.getProductId();
		int pinaid = 0;
		if (pinaids != null && pinaids.size() > 0) {
			pinaid = Integer.valueOf(pinaids.get(0));
		}
		Logger.comment("开始校验商品信息Products");
		Map<String, Object> productMap = new ProductsWapperV2().getByProductId(productId);
		// 允许时间差值 5s
		long candiff = 5 * 1000;
		// 是否有活动
		boolean hasactivity = pinaids.size() > 0;
		// 组装数据
		// - 商品信息
		Logger.verifyEquals(product.getProdId(), productMap.get("ipid"), "验证ipid");
		// ProductInDetailBean productInDetailBean = new ProductInDetailBean();
		// productInDetailBean.setProdId(Integer.parseInt(productMap.get("ipid").toString()));
		// productInDetailBean.setProductId(productId);
		Logger.verifyEquals(product.getVersion(), String.valueOf(Double.valueOf(productMap.get("ver").toString()) / 1000), "验证ver");
		// productInDetailBean.setVersion(String.valueOf(Double.valueOf(productMap.get("ver").toString())
		// / 1000));
		Logger.verifyEquals(product.getTitle(), productMap.get("title").toString(), "验证title");
		// productInDetailBean.setTitle(productMap.get("title").toString());
		Logger.verifyEquals(product.getIntegerroduction(), productMap.get("intro"), "验证intro");
		// productInDetailBean.setIntroduction(productMap.get("intro") == null ?
		// "" : productMap.get("intro").toString());
		if (!hasactivity) {
			// Logger.verifyEquals(product.getValidStart(),(Date)
			// productMap.get("start"),"验证start");
			Logger.verifyEquals(true, vdate(product.getValidStart(), (Date) productMap.get("start"), candiff), "验证start");
			// productInDetailBean.setValidStart((Date)
			// productMap.get("start"));
			Logger.verifyEquals(true, vdate(product.getValidEnd(), (Date) productMap.get("end"), candiff), "验证end");
		}
		// Logger.verifyEquals(product.getValidEnd(),(Date)
		// productMap.get("end"),"验证start");

		// productInDetailBean.setValidEnd((Date) productMap.get("end"));
		Logger.verifyEquals(product.getSellerId(), productMap.get("sid"), "验证sid");
		// productInDetailBean.setSellerId(Integer.parseInt(productMap.get("sid").toString()));
		Logger.verifyEquals(product.getSellerName(), productMap.get("sname"), "验证sname");
		// productInDetailBean.setSellerName(productMap.get("sname").toString());
		Logger.verifyEquals(product.getBrandId(), productMap.get("bid"), "验证bid");
		// productInDetailBean.setBrandId(Integer.parseInt(productMap.get("bid").toString()));
		Logger.verifyEquals(product.getBrandName(), productMap.get("brand"), "验证brand");
		// productInDetailBean.setBrandName(productMap.get("brand").toString());
		Logger.verifyEquals(product.getBrandEnName(), productMap.get("ebrand"), "验证ebrand");
		// productInDetailBean.setBrandEnName(productMap.get("ebrand").toString());
		Logger.verifyEquals(product.getMasterCategoryId(), productMap.get("mcatid"), "验证mcatid");
		// productInDetailBean.setMasterCategoryId(Integer.parseInt(productMap.get("mcatid").toString()));
		Logger.verifyEquals(product.getMasterCategoryName(), productMap.get("mcatname"), "验证mcatname");
		// productInDetailBean.setMasterCategoryName(productMap.get("mcatname").toString());
		Logger.verifyEquals(product.getCategoryId(), productMap.get("scatid"), "验证scatid");
		// productInDetailBean.setCategoryId(Integer.parseInt(productMap.get("scatid").toString()));
		Logger.verifyEquals(product.getCategoryName(), productMap.get("scatname"), "验证scatname");
		// productInDetailBean.setCategoryName(productMap.get("scatname").toString());
		Logger.verifyEquals(product.getThirdCategoryId(), productMap.get("tcatid"), "验证tcatid");
		// productInDetailBean.setThirdCategoryId(Integer.parseInt(productMap.get("tcatid").toString()));
		Logger.verifyEquals(product.getThirdCategoryName(), productMap.get("tcatname"), "验证tcatname");
		// productInDetailBean.setThirdCategoryName(productMap.get("tcatname").toString());
		Logger.verifyEquals(product.getCatalogType(), productMap.get("ctype"), "验证ctype");
		// productInDetailBean.setCatalogType(Integer.parseInt(productMap.get("ctype").toString()));
		Logger.verifyEquals(product.getDeliveryMethod(), productMap.get("deliv"), "验证deliv");
		// productInDetailBean.setDeliveryMethod(Integer.parseInt(productMap.get("deliv").toString()));
		Logger.verifyEquals(product.getBondedArea(), productMap.get("bonded"), "验证bonded");
		// productInDetailBean.setBondedArea(Integer.parseInt(productMap.get("bonded").toString()));
		Logger.verifyEquals(product.getTariffType(), productMap.get("tariffy"), "验证tariffy");
		// productInDetailBean.setTariffType(Integer.parseInt(productMap.get("tariffy").toString()));
		Logger.verifyEquals(product.getIsFreeShipping(), Double.parseDouble(productMap.get("shipping").toString()) <= 0, "验证tariffy");
		// productInDetailBean.setIsFreeShipping(Double.parseDouble(productMap.get("shipping").toString())
		// <= 0);
		Logger.verifyEquals(product.getCountryId(), productMap.get("country"), "验证country");
		// productInDetailBean.setCountryId(Integer.parseInt(productMap.get("country").toString()));
		// Logger.verifyEquals(product.getAddTime(),((Date)productMap.get("addtime")),"验证addtime");
		Logger.verifyEquals(true, vdate(product.getAddTime(), (Date) productMap.get("addtime"), candiff), "验证addtime");
		// productInDetailBean.setAddTime((Date) productMap.get("addtime"));
		Logger.verifyEquals(product.getLocalReturn(), productMap.get("localr"), "验证localr");
		// productInDetailBean.setLocalReturn(Integer.parseInt(productMap.get("localr").toString()));
		Logger.verifyEquals(product.getNoticeRisk(), productMap.get("risk"), "验证risk");
		// productInDetailBean.setNoticeRisk(Boolean.parseBoolean(productMap.get("risk").toString()));
		Logger.verifyEquals(product.getProductCode(), productMap.get("pcode") == null ? "" : productMap.get("pcode").toString(), "验证pcode");
		// productInDetailBean.setProductCode(productMap.get("pcode") == null ?
		// "" : productMap.get("pcode").toString());
		Logger.verifyEquals(product.getNoReasonReturn(), productMap.get("noreason"), "验证noreason");
		// productInDetailBean.setNoReasonReturn(Boolean.parseBoolean(productMap.get("noreason").toString()));
		Logger.verifyEquals(product.getIsNewProduct(), productMap.get("isnew"), "验证isnew");
		// productInDetailBean
		// .setIsNewProduct(getNewestProduct(Boolean.parseBoolean(productMap.get("isnew").toString()),
		// (Date) productMap.get("newstart"), (Date) productMap.get("newend")));
		//
		// Logger.verifyEquals(product.getNewStartTime(),
		// (Date)productMap.get("newstart"), "验证newstart");
		Logger.verifyEquals(true, vdate(product.getNewStartTime(), (Date) productMap.get("newstart"), candiff), "验证newstart");
		// productInDetailBean.setNewStartTime((Date)
		// productMap.get("newstart"));
		// Logger.verifyEquals(product.getNewEndTime(),
		// (Date)productMap.get("newend"), "验证newend");
		Logger.verifyEquals(true, vdate(product.getNewEndTime(), (Date) productMap.get("newend"), candiff), "验证newend");
		// productInDetailBean.setNewEndTime((Date) productMap.get("newend"));
		Logger.verifyEquals(product.getIsHotRecmd(), productMap.get("istop"), "验证istop");
		// productInDetailBean.setIsHotRecmd(Boolean.parseBoolean(productMap.get("istop").toString()));
		Logger.verifyEquals(product.getIsPspProduct(), productMap.get("ispsp"), "验证ispsp");
		// productInDetailBean.setIsPspProduct(Boolean.parseBoolean(productMap.get("ispsp").toString()));
		Logger.verifyEquals(product.getGrade(), productMap.get("grade"), "验证grade");
		// productInDetailBean.setGrade(productMap.get("grade").toString());
		Logger.verifyEquals(product.getHasTextDescription(), productMap.get("newdesc"), "验证newdesc");
		// productInDetailBean.setHasTextDescription(Boolean.parseBoolean(productMap.get("newdesc").toString()));
		//
		// // video
		if (productMap.containsKey("v") && productMap.get("v") != null) {
			JSONObject jsonObject = new JSONObject(productMap.get("v").toString());
			//
			// ProductVideoBean productVideoBean = new ProductVideoBean();
			Logger.verifyEquals(product.getVideo().getVideoUrl(), jsonObject.getString("u"), "验证u");
			// productVideoBean.setVideoUrl(jsonObject.getString("u"));
			Logger.verifyEquals(product.getVideo().getVideoLength(), jsonObject.getString("l"), "验证l");
			// productVideoBean.setVideoLength(jsonObject.getInt("l"));
			Logger.verifyEquals(product.getVideo().getVideoWidth(), jsonObject.getString("w"), "验证w");
			// productVideoBean.setVideoWidth(jsonObject.getInt("w"));
			Logger.verifyEquals(product.getVideo().getVideoHeight(), jsonObject.getString("h"), "验证h");
			// productVideoBean.setVideoHeight(jsonObject.getInt("h"));
			Logger.verifyEquals(product.getVideo().getVideoSize(), jsonObject.getString("s"), "验证s");
			// productVideoBean.setVideoSize(jsonObject.getInt("s"));
			//
			// productInDetailBean.setVideo(productVideoBean);
			//
		}
		//
		// // pic list
		List<String> picList = new ArrayList<>();
		if (productMap.get("pics") != null) {
			JSONArray jsonArray = new JSONArray(productMap.get("pics").toString());
			for (int i = 0; i < jsonArray.length(); i++) {
				picList.add(jsonArray.getString(i));
			}
		}
		Logger.verifyEquals(product.getPicList(), picList, "验证picList");
		// productInDetailBean.setPicList(picList);
		//
		// // SizePicList
		List<String> sizePics = new ArrayList<>();
		if (productMap.get("sizepics") != null) {
			JSONArray jsonArray = new JSONArray(productMap.get("sizepics").toString());
			for (int i = 0; i < jsonArray.length(); i++) {
				sizePics.add(jsonArray.getString(i));
			}
		}
		Logger.verifyEquals(product.getSizePicList(), sizePics, "验证sizePics");
		// productInDetailBean.setSizePicList(sizePics);
		// productInDetailBean.setStatus(getProductStatus(productId,
		// prodInActivityId, liveId));
		//
		Logger.verifyEquals(product.getOwnProduct(), Integer.parseInt(productMap.get("own").toString()) == 1 ? true : false, "验证own");
		// productInDetailBean.setOwnProduct(Integer.parseInt(productMap.get("own").toString())
		// == 1 ? true : false);
		Logger.verifyEquals(product.getExtraDeliveryType(), productMap.get("mdeliv"), "验证mdeliv");
		// productInDetailBean.setExtraDeliveryType(Integer.parseInt(productMap.get("mdeliv").toString()));
		Logger.verifyEquals(product.getExtraDeliveryFee(), productMap.get("mflight"), "验证mflight");
		// productInDetailBean.setExtraDeliveryFee(Double.parseDouble(productMap.get("mflight").toString()));
		// 规格信息
		List<CatalogDto> catalist = product.getCatalogList();
		Map<String, Map> mongocatalist = new HashMap<String, Map>();
		new CatalogsWapper().getAllByProductId(productId).stream().forEach(map -> {
			mongocatalist.put(map.get("cid").toString(), map);
		});
		Logger.verifyEquals(catalist.size(), mongocatalist.size(), "验证规格size");
		// 活动价格没在这里验证
		for (int i = 0; i < catalist.size(); i++) {
			CatalogDto rcata = catalist.get(i);
			Map mcata = mongocatalist.get(rcata.getCatalogId());
			Logger.verifyNotNull(mcata, "验证mongo有数据");
			Logger.verifyEquals(rcata.getSKU(), mcata.get("sku"), "验证sku");
			Logger.verifyEquals(rcata.getPrice(), mcata.get("price"), "验证price");
			Logger.verifyEquals(rcata.getNewGuestPrice(), mcata.get("newp"), "验证newp");
			Logger.verifyEquals(rcata.getVipPrice(), mcata.get("vip"), "验证vip");
			List<Map> mprops = (List<Map>) mcata.get("props");
			Logger.verifyEquals(rcata.getPropertyList().size(), mprops.size(), "验证PropertyList.size");
			List<String> names = rcata.getPropertyList().stream().map(CatalogPropertyDto::getName).collect(Collectors.toList());
			List<String> values = rcata.getPropertyList().stream().map(CatalogPropertyDto::getValue).collect(Collectors.toList());
			List<String> pics = rcata.getPropertyList().stream().map(CatalogPropertyDto::getPicUrl).collect(Collectors.toList());
			mprops.stream().forEach(mprop -> {
				Logger.verifyEquals(true, names.contains(mprop.get("name")), "验证name");
				Logger.verifyEquals(true, values.contains(mprop.get("value")), "验证value");
				Logger.verifyEquals(true, pics.contains(mprop.get("pic")), "验证pic");
			});
		}
		// 活动
		ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
		// 售罄规格
		SellOutActivityDto soa = product.getSellOutActivity();
		// 活动-非售罄
		if (hasactivity) {
			ArrayList<Map> mapss = activityProductsWapper.getByProdInActivityIds(pinaid);
			for (Map maps : mapss) {
				// 开始结束时间
				Date starttime = (Date) maps.get("start");
				Date endtime = (Date) maps.get("end");
				// 获取活动规格
				List<Map> catalogsAry = (List<Map>) FormatUtil.GSONToObject(maps.get("catalogs").toString(), new TypeToken<List<Map>>() {
				}.getType());
				Stream<Map> hasStock = catalogsAry.stream().filter(mapper -> (Double.valueOf(mapper.get("stock").toString()) > 0));
				Double max = catalogsAry.stream().map(mapper -> Double.valueOf(mapper.get("price").toString())).max(Comparator.comparing(u -> u)).get();
				Double min = catalogsAry.stream().map(mapper -> Double.valueOf(mapper.get("price").toString())).min(Comparator.comparing(u -> u)).get();
				List cids = hasStock.map(mapper -> mapper.get("cid")).collect(Collectors.toList());
				//如果售罄 不会出现在本场和下一次
				if (cids == null || cids.size() == 0) {
					SellOutActivityDto atv = product.getSellOutActivity();
					Logger.verifyEquals(atv.getActivityId(), maps.get("aid"), "验证aid");
					Logger.verifyEquals(atv.getActivityName(), maps.get("aname"), "验证aname");
					Logger.verifyEquals(atv.getActivityType(), maps.get("type") == null ? 0 : maps.get("type"), "验证type");
					
					Logger.verifyEquals(atv.getMarketPrice(), Double.valueOf(maps.get("market").toString()), "验证market");
					Logger.verifyEquals(atv.getMaxPrice(), max, "验证MaxPrice");
					Logger.verifyEquals(atv.getMinPrice(), min, "验证MinPrice");
					Logger.verifyEquals(atv.getActivityType(), maps.get("atype") == null ? 0 : maps.get("atype"), "验证atype");
				} else {
					if (starttime.getTime() > YMTDateUtil.getDate().getTime()) {
						// 未来-next
						ProductActivityDto atv = product.getNextActivity();
						Logger.verifyEquals(atv.getActivityId(), maps.get("aid"), "验证aid");
						Logger.verifyEquals(atv.getActivityLimit(), maps.get("alimit"), "验证alimit");
						Logger.verifyEquals(atv.getActivityName(), maps.get("aname"), "验证aname");
						Logger.verifyEquals(atv.getActivityProductLimit(), maps.get("plimit"), "验证plimit");
						Logger.verifyEquals(true, vdate(atv.getBeginTimeOfProductInActivity(), starttime, candiff), "验证BeginTimeOfProductInActivity");
						Logger.verifyEquals(true, vdate(atv.getEndTimeOfProductInActivity(), endtime, candiff), "验证EndTimeOfProductInActivity");
						Logger.verifyEquals(atv.getIsPartCatalogs(), maps.get("part"), "验证part");
						Logger.verifyEquals(atv.getIsTradeIsolation(), maps.get("isolation"), "验证isolation");
						Logger.verifyEquals(atv.getMarketPrice(), Double.valueOf(maps.get("market").toString()), "验证market");
						Logger.verifyEquals(atv.getMaxActivityPrice(), max, "验证MaxPrice");
						Logger.verifyEquals(atv.getMinActivityPrice(), min, "验证MinPrice");
						Logger.verifyEquals(atv.getActivityType(), maps.get("atype") == null ? 0 : maps.get("atype"), "验证atype");
						Logger.verifyEquals(atv.getSourceId(), maps.get("sourceId") == null ? 0 : maps.get("sourceId"), "验证sourceId");
						// Promotion Promotion Type ?
						Logger.debug("catalogsAry:" + catalogsAry);
						Logger.verifyEquals(atv.getCatalogList().size(), cids.size(), "验证CatalogList-size");
						cids.forEach(cid -> {
							Logger.verifyEquals(true, atv.getCatalogList().contains(cid), "验证包含catalogid:" + cid);
						});
					} else {
						// 当前活动
						ProductActivityDto atv = product.getProductActivity();
						Logger.verifyEquals(atv.getActivityId(), maps.get("aid"), "验证aid");
						Logger.verifyEquals(atv.getActivityLimit(), maps.get("alimit"), "验证alimit");
						Logger.verifyEquals(atv.getActivityName(), maps.get("aname"), "验证aname");
						Logger.verifyEquals(atv.getActivityProductLimit(), maps.get("plimit"), "验证plimit");
						Logger.verifyEquals(true, vdate(atv.getBeginTimeOfProductInActivity(), starttime, candiff), "验证BeginTimeOfProductInActivity");
						Logger.verifyEquals(true, vdate(atv.getEndTimeOfProductInActivity(), endtime, candiff), "验证EndTimeOfProductInActivity");
						Logger.verifyEquals(atv.getIsPartCatalogs(), maps.get("part"), "验证part");
						Logger.verifyEquals(atv.getIsTradeIsolation(), maps.get("isolation"), "验证isolation");
						Logger.verifyEquals(atv.getMarketPrice(), Double.valueOf(maps.get("market").toString()), "验证market");
						Logger.verifyEquals(atv.getMaxActivityPrice(), max, "验证MaxPrice");
						Logger.verifyEquals(atv.getMinActivityPrice(), min, "验证MinPrice");
						Logger.verifyEquals(atv.getActivityType(), maps.get("atype") == null ? 0 : maps.get("atype"), "验证atype");
						Logger.verifyEquals(atv.getSourceId(), maps.get("sourceId") == null ? 0 : maps.get("sourceId"), "验证sourceId");
						// Promotion Promotion Type ?
						Logger.debug("catalogsAry:" + catalogsAry);
						Logger.verifyEquals(atv.getCatalogList().size(), cids.size(), "验证CatalogList-size");
						cids.forEach(cid -> {
							Logger.verifyEquals(true, atv.getCatalogList().contains(cid), "验证包含catalogid:" + cid);
						});
					}
				}

			}
		}
		
	}

	/**
	 * @param product
	 *            返回报文
	 * @param inaids
	 *            参加活动商品inaid
	 * @param liveId
	 *            活动id
	 * @throws Exception
	 */
	private void verifyDB(ProductDetailDto2 product, List<String> inaids, Integer liveId) throws Exception {
		String productId = product.getProductId();
		int inaid = 0;
		if (inaids != null && inaids.size() > 0) {
			inaid = Integer.valueOf(inaids.get(0));
		}
		Logger.comment("开始校验商品信息Products");
		Map<String, Object> productMap = new ProductsWapperV2().getByProductId(productId);
		// 允许时间差值 5s
		long candiff = 5 * 1000;
		// 是否有活动
		boolean hasactivity = inaids.size() > 0;
		// 组装数据
		// - 商品信息
		Logger.verifyEquals(product.getProdId(), productMap.get("ipid"), "验证ipid");
		Logger.verifyEquals(product.getVersion(), String.valueOf(Double.valueOf(productMap.get("ver").toString()) / 1000), "验证ver");
		Logger.verifyEquals(product.getTitle(), productMap.get("title").toString(), "验证title");
		Logger.verifyEquals(product.getIntegerroduction(), productMap.get("intro"), "验证intro");
		if (!hasactivity) {
			Logger.verifyEquals(true, vdate(product.getValidStart(), (Date) productMap.get("start"), candiff), "验证start");
			Logger.verifyEquals(true, vdate(product.getValidEnd(), (Date) productMap.get("end"), candiff), "验证end");
		}
		Logger.verifyEquals(product.getSellerId(), productMap.get("sid"), "验证sid");
		Logger.verifyEquals(product.getSellerName(), productMap.get("sname"), "验证sname");
		Logger.verifyEquals(product.getBrandId(), productMap.get("bid"), "验证bid");
		Logger.verifyEquals(product.getBrandName(), productMap.get("brand"), "验证brand");
		Logger.verifyEquals(product.getBrandEnName(), productMap.get("ebrand"), "验证ebrand");
		Logger.verifyEquals(product.getMasterCategoryId(), productMap.get("mcatid"), "验证mcatid");
		Logger.verifyEquals(product.getMasterCategoryName(), productMap.get("mcatname"), "验证mcatname");
		Logger.verifyEquals(product.getCategoryId(), productMap.get("scatid"), "验证scatid");
		Logger.verifyEquals(product.getCategoryName(), productMap.get("scatname"), "验证scatname");
		Logger.verifyEquals(product.getThirdCategoryId(), productMap.get("tcatid"), "验证tcatid");
		Logger.verifyEquals(product.getThirdCategoryName(), productMap.get("tcatname"), "验证tcatname");
		Logger.verifyEquals(product.getCatalogType(), productMap.get("ctype"), "验证ctype");
		Logger.verifyEquals(product.getDeliveryMethod(), productMap.get("deliv"), "验证deliv");
		Logger.verifyEquals(product.getBondedArea(), productMap.get("bonded"), "验证bonded");
		Logger.verifyEquals(product.getTariffType(), productMap.get("tariffy"), "验证tariffy");
		Logger.verifyEquals(product.getIsFreeShipping(), Double.parseDouble(productMap.get("shipping").toString()) <= 0, "验证tariffy");
		Logger.verifyEquals(product.getCountryId(), productMap.get("country"), "验证country");
		Logger.verifyEquals(true, vdate(product.getAddTime(), (Date) productMap.get("addtime"), candiff), "验证addtime");
		Logger.verifyEquals(product.getLocalReturn(), productMap.get("localr"), "验证localr");
		Logger.verifyEquals(product.getNoticeRisk(), productMap.get("risk"), "验证risk");
		Logger.verifyEquals(product.getProductCode(), productMap.get("pcode") == null ? "" : productMap.get("pcode").toString(), "验证pcode");
		Logger.verifyEquals(product.getNoReasonReturn(), productMap.get("noreason"), "验证noreason");
		Logger.verifyEquals(product.getIsNewProduct(), productMap.get("isnew"), "验证isnew");

		Logger.verifyEquals(true, vdate(product.getNewStartTime(), (Date) productMap.get("newstart"), candiff), "验证newstart");
		Logger.verifyEquals(true, vdate(product.getNewEndTime(), (Date) productMap.get("newend"), candiff), "验证newend");
		Logger.verifyEquals(product.getIsHotRecmd(), productMap.get("istop"), "验证istop");
		Logger.verifyEquals(product.getIsPspProduct(), productMap.get("ispsp"), "验证ispsp");
		Logger.verifyEquals(product.getGrade(), productMap.get("grade"), "验证grade");
		Logger.verifyEquals(product.getHasTextDescription(), productMap.get("newdesc"), "验证newdesc");
		if (productMap.containsKey("v") && productMap.get("v") != null) {
			JSONObject jsonObject = new JSONObject(productMap.get("v").toString());
			Logger.verifyEquals(product.getVideo().getVideoUrl(), jsonObject.getString("u"), "验证u");
			Logger.verifyEquals(product.getVideo().getVideoLength(), jsonObject.getString("l"), "验证l");
			Logger.verifyEquals(product.getVideo().getVideoWidth(), jsonObject.getString("w"), "验证w");
			Logger.verifyEquals(product.getVideo().getVideoHeight(), jsonObject.getString("h"), "验证h");
			Logger.verifyEquals(product.getVideo().getVideoSize(), jsonObject.getString("s"), "验证s");
		}
		//
		// // pic list
		List<String> picList = new ArrayList<>();
		if (productMap.get("pics") != null) {
			JSONArray jsonArray = new JSONArray(productMap.get("pics").toString());
			for (int i = 0; i < jsonArray.length(); i++) {
				picList.add(jsonArray.getString(i));
			}
		}
		Logger.verifyEquals(product.getPicList(), picList, "验证picList");
		//
		// // SizePicList
		List<String> sizePics = new ArrayList<>();
		if (productMap.get("sizepics") != null) {
			JSONArray jsonArray = new JSONArray(productMap.get("sizepics").toString());
			for (int i = 0; i < jsonArray.length(); i++) {
				sizePics.add(jsonArray.getString(i));
			}
		}
		Logger.verifyEquals(product.getSizePicList(), sizePics, "验证sizePics");
		// productInDetailBean.setSizePicList(sizePics);
		// 这里可能有问题 需要再确认
		Logger.verifyEquals(product.getStatus(), getProductStatus(productId, inaid, liveId), "验证sizePics");
		// productInDetailBean.setStatus(getProductStatus(productId,
		// prodInActivityId, liveId));
		//
		Logger.verifyEquals(product.getOwnProduct(), Integer.parseInt(productMap.get("own").toString()) == 1 ? true : false, "验证own");
		// productInDetailBean.setOwnProduct(Integer.parseInt(productMap.get("own").toString())
		// == 1 ? true : false);
		Logger.verifyEquals(product.getExtraDeliveryType(), productMap.get("mdeliv"), "验证mdeliv");
		// productInDetailBean.setExtraDeliveryType(Integer.parseInt(productMap.get("mdeliv").toString()));
		Logger.verifyEquals(product.getExtraDeliveryFee(), productMap.get("mflight"), "验证mflight");
		// productInDetailBean.setExtraDeliveryFee(Double.parseDouble(productMap.get("mflight").toString()));
		// 规格信息
		List<CatalogDto> catalist = product.getCatalogList();
		Map<String, Map> mongocatalist = new HashMap<String, Map>();
		new CatalogsWapper().getAllByProductId(productId).stream().forEach(map -> {
			mongocatalist.put(map.get("cid").toString(), map);
		});
		Logger.verifyEquals(catalist.size(), mongocatalist.size(), "验证规格size");
		// 活动价格没在这里验证
		for (int i = 0; i < catalist.size(); i++) {
			CatalogDto rcata = catalist.get(i);
			Map mcata = mongocatalist.get(rcata.getCatalogId());
			Logger.verifyNotNull(mcata, "验证mongo有数据");
			Logger.verifyEquals(rcata.getSKU(), mcata.get("sku"), "验证sku");
			Logger.verifyEquals(rcata.getPrice(), mcata.get("price"), "验证price");
			Logger.verifyEquals(rcata.getNewGuestPrice(), mcata.get("newp"), "验证newp");
			Logger.verifyEquals(rcata.getVipPrice(), mcata.get("vip"), "验证vip");
			List<Map> mprops = (List<Map>) mcata.get("props");
			Logger.verifyEquals(rcata.getPropertyList().size(), mprops.size(), "验证PropertyList.size");
			List<String> names = rcata.getPropertyList().stream().map(CatalogPropertyDto::getName).collect(Collectors.toList());
			List<String> values = rcata.getPropertyList().stream().map(CatalogPropertyDto::getValue).collect(Collectors.toList());
			List<String> pics = rcata.getPropertyList().stream().map(CatalogPropertyDto::getPicUrl).collect(Collectors.toList());
			mprops.stream().forEach(mprop -> {
				Logger.verifyEquals(true, names.contains(mprop.get("name")), "验证name");
				Logger.verifyEquals(true, values.contains(mprop.get("value")), "验证value");
				Logger.verifyEquals(true, pics.contains(mprop.get("pic")), "验证pic");
			});
		}
		// 活动
		ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
		// 售罄规格
		SellOutActivityDto soa = product.getSellOutActivity();
		// 活动-非售罄
		if (hasactivity) {
			ArrayList<Map> mapss = activityProductsWapper.getByProdInActivityIds(inaid);
			for (Map maps : mapss) {
				// 开始结束时间
				Date starttime = (Date) maps.get("start");
				Date endtime = (Date) maps.get("end");
				// 获取活动规格
				List<Map> catalogsAry = (List<Map>) FormatUtil.GSONToObject(maps.get("catalogs").toString(), new TypeToken<List<Map>>() {
				}.getType());
				Stream<Map> hasStock = catalogsAry.stream().filter(mapper -> (Double.valueOf(mapper.get("stock").toString()) > 0));
				Double max = catalogsAry.stream().map(mapper -> Double.valueOf(mapper.get("price").toString())).max(Comparator.comparing(u -> u)).get();
				Double min = catalogsAry.stream().map(mapper -> Double.valueOf(mapper.get("price").toString())).min(Comparator.comparing(u -> u)).get();
				List cids = hasStock.map(mapper -> mapper.get("cid")).collect(Collectors.toList());
				//如果售罄 不会出现在本场和下一次
				if (cids == null || cids.size() == 0) {
					SellOutActivityDto atv = product.getSellOutActivity();
					Logger.verifyEquals(atv.getActivityId(), maps.get("aid"), "验证aid");
					Logger.verifyEquals(atv.getActivityName(), maps.get("aname"), "验证aname");
					Logger.verifyEquals(atv.getActivityType(), maps.get("type") == null ? 0 : maps.get("type"), "验证type");
					
					Logger.verifyEquals(atv.getMarketPrice(), Double.valueOf(maps.get("market").toString()), "验证market");
					Logger.verifyEquals(atv.getMaxPrice(), max, "验证MaxPrice");
					Logger.verifyEquals(atv.getMinPrice(), min, "验证MinPrice");
					Logger.verifyEquals(atv.getActivityType(), maps.get("atype") == null ? 0 : maps.get("atype"), "验证atype");
				} else {
					if (starttime.getTime() > YMTDateUtil.getDate().getTime()) {
						// 未来-next
						ProductActivityDto atv = product.getNextActivity();
						Logger.verifyEquals(atv.getActivityId(), maps.get("aid"), "验证aid");
						Logger.verifyEquals(atv.getActivityLimit(), maps.get("alimit"), "验证alimit");
						Logger.verifyEquals(atv.getActivityName(), maps.get("aname"), "验证aname");
						Logger.verifyEquals(atv.getActivityProductLimit(), maps.get("plimit"), "验证plimit");
						Logger.verifyEquals(true, vdate(atv.getBeginTimeOfProductInActivity(), starttime, candiff), "验证BeginTimeOfProductInActivity");
						Logger.verifyEquals(true, vdate(atv.getEndTimeOfProductInActivity(), endtime, candiff), "验证EndTimeOfProductInActivity");
						Logger.verifyEquals(atv.getIsPartCatalogs(), maps.get("part"), "验证part");
						Logger.verifyEquals(atv.getIsTradeIsolation(), maps.get("isolation"), "验证isolation");
						Logger.verifyEquals(atv.getMarketPrice(), Double.valueOf(maps.get("market").toString()), "验证market");
						Logger.verifyEquals(atv.getMaxActivityPrice(), max, "验证MaxPrice");
						Logger.verifyEquals(atv.getMinActivityPrice(), min, "验证MinPrice");
						Logger.verifyEquals(atv.getActivityType(), maps.get("atype") == null ? 0 : maps.get("atype"), "验证atype");
						Logger.verifyEquals(atv.getSourceId(), maps.get("sourceId") == null ? 0 : maps.get("sourceId"), "验证sourceId");
						// Promotion Promotion Type ?
						Logger.debug("catalogsAry:" + catalogsAry);
						Logger.verifyEquals(atv.getCatalogList().size(), cids.size(), "验证CatalogList-size");
						cids.forEach(cid -> {
							Logger.verifyEquals(true, atv.getCatalogList().contains(cid), "验证包含catalogid:" + cid);
						});
					} else {
						// 当前活动
						ProductActivityDto atv = product.getProductActivity();
						Logger.verifyEquals(atv.getActivityId(), maps.get("aid"), "验证aid");
						Logger.verifyEquals(atv.getActivityLimit(), maps.get("alimit"), "验证alimit");
						Logger.verifyEquals(atv.getActivityName(), maps.get("aname"), "验证aname");
						Logger.verifyEquals(atv.getActivityProductLimit(), maps.get("plimit"), "验证plimit");
						Logger.verifyEquals(true, vdate(atv.getBeginTimeOfProductInActivity(), starttime, candiff), "验证BeginTimeOfProductInActivity");
						Logger.verifyEquals(true, vdate(atv.getEndTimeOfProductInActivity(), endtime, candiff), "验证EndTimeOfProductInActivity");
						Logger.verifyEquals(atv.getIsPartCatalogs(), maps.get("part"), "验证part");
						Logger.verifyEquals(atv.getIsTradeIsolation(), maps.get("isolation"), "验证isolation");
						Logger.verifyEquals(atv.getMarketPrice(), Double.valueOf(maps.get("market").toString()), "验证market");
						Logger.verifyEquals(atv.getMaxActivityPrice(), max, "验证MaxPrice");
						Logger.verifyEquals(atv.getMinActivityPrice(), min, "验证MinPrice");
						Logger.verifyEquals(atv.getActivityType(), maps.get("atype") == null ? 0 : maps.get("atype"), "验证atype");
						Logger.verifyEquals(atv.getSourceId(), maps.get("sourceId") == null ? 0 : maps.get("sourceId"), "验证sourceId");
						// Promotion Promotion Type ?
						Logger.debug("catalogsAry:" + catalogsAry);
						Logger.verifyEquals(atv.getCatalogList().size(), cids.size(), "验证CatalogList-size");
						cids.forEach(cid -> {
							Logger.verifyEquals(true, atv.getCatalogList().contains(cid), "验证包含catalogid:" + cid);
						});
					}
				}

			}
		}

	}

	/**
	 * 时间是否一致
	 * 
	 * @param d1
	 * @param d2
	 * @param candiff
	 *            ms
	 * @return
	 */
	private static boolean vdate(Date d1, Date d2, long candiff) {
		boolean v = false;
		if (Math.abs(d1.getTime() - d2.getTime()) < candiff) {
			v = true;
		} else {
			Logger.debug("too long diff time:" + d1.getTime() + ":" + d2.getTime());
		}
		return v;
	}

	private static int getProductStatus(String productId, int prodInActivityId, int liveId) throws Exception {
		ProductsWapperV2 productsWapper = new ProductsWapperV2();
		Map<String, Object> productInMgo = productsWapper.getByProductId(productId);
		Date now = YMTDateUtil.getDateBeforeOrNextSecond(300);

		// 商品删除
		if (Integer.parseInt(productInMgo.get("action").toString()) == -1) {
			return -1;
		}

		// 有活动
		if (prodInActivityId > 0) {
			return 0;
		}

		// 有直播
		if (liveId > 0) {
			LiveProductsWapper liveProductsWapper = new LiveProductsWapper();
			Map<String, Object> liveProdInMgo = liveProductsWapper.getByActivityId(liveId);
			if (now.after((Date) liveProdInMgo.get("start")) && now.before((Date) liveProdInMgo.get("end")) && (Integer.parseInt(liveProdInMgo.get("status").toString()) == 1)) {
				return 0;
			} else {
				return 2;
			}
		}

		// 商品
		if (now.after((Date) productInMgo.get("start")) && now.before((Date) productInMgo.get("end"))) {
			return 0;
		} else {
			return 2;
		}

	}
}
